package com.JZ.QM.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;

import com.JZ.QM.common.dao.CounselorInfoDao;
import com.JZ.QM.common.entity.CounselorInfoEntity;
import com.JZ.QM.service.CounselorInfoService;

import javax.annotation.Resource;

/**
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020/6/15 9:13
 */

@Slf4j
@Service
public class CounselorInfoServiceImpl extends ServiceImpl<CounselorInfoDao, CounselorInfoEntity> implements CounselorInfoService {

    @Resource
    CounselorInfoDao counselorInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<CounselorInfoEntity> page = this.page(
                new Query<CounselorInfoEntity>().getPage(params),
                new QueryWrapper<CounselorInfoEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 登录功能
     * @param accountCode 账户代码
     * @param password 密码
     */
    @Override
    public boolean login(String accountCode, String password) {
        //查询条件构造器
        QueryWrapper<CounselorInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("counselor_account",accountCode);

        CounselorInfoEntity counselorInfoEntity = counselorInfoDao.selectOne(queryWrapper);
        //账号不存在
        if(counselorInfoEntity == null){
            return false;
        }

        //密码不符合
        if (password.equals(counselorInfoEntity.getCounselorPassword())){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 为前端cookie设置name和id属性
     * @param accountCode 账户id
     */
    @Override
    public Map<String,String> getCookie(String accountCode) {
        //查询条件构造器
        QueryWrapper<CounselorInfoEntity> queryWrapper = new QueryWrapper<>();
        //投顾人员名称
        String name = "";
        //投顾人员id
        Long id = Long.valueOf(0);
        //结果集封装
        Map<String,String> result = new HashMap<>();

        //获取对应的参数
        queryWrapper.eq("counselor_account",accountCode);
        CounselorInfoEntity counselorInfoEntity = counselorInfoDao.selectOne(queryWrapper);

        if(counselorInfoEntity != null){
            name = counselorInfoEntity.getCounselorName();
            id = counselorInfoEntity.getCounselorId();
            result.put("name",name);
            result.put("id", String.valueOf(id));
        }
        return result;
    }

}